// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: Animal Prison Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

// Aliases
private alias 22 : TYPE_ANIMALPRISON

// Function declarations
reserve function AnimalPrison_DebugDraw
reserve function AnimalPrison_DebugSpawn

function AnimalPrison_DebugDraw
	DrawSprite(3)
	DrawSprite(0)
	DrawSprite(4)
end function


function AnimalPrison_DebugSpawn
	CreateTempObject(TypeName[Animal Prison], 0, object.xpos, object.ypos)
end function


event ObjectMain
	switch object.state
	case 0
		temp0 = object.ixpos
		temp0 -= screen.xcenter
		stage.newXBoundary1 = temp0
		temp0 = object.ixpos
		temp0 += screen.xcenter
		stage.newXBoundary2 = temp0
		break

	case 1
		object.value0++
		object.value3 += object.xvel
		object.value4 += object.yvel
		object.yvel += 0x4000
		if object.value0 == 30
			temp1 = object.ypos
			temp1 += 0x40000
			temp2 = 0
			temp3 = 30
			while temp2 < 32
				Rand(temp0, 48)
				temp0 >>= 2
				temp0 <<= 18
				temp0 -= 0x180000
				temp0 += object.xpos
				temp4 = temp2
				temp4 &= 1
				if temp4 == false
					CreateTempObject(animalType1, temp3, temp0, temp1)
				else
					CreateTempObject(animalType2, temp3, temp0, temp1)
				end if
				object[tempObjectPos].value1 = 1
				temp0 += 0x40000
				temp2++
				temp3 += 5
			loop
			object.drawOrder = 4
			object.value0 = 0
			object.state++
			object.value5 = 5
			object.value6 = 17
		end if
		break

	case 2
		object.value5 = object.value0
		object.value5 += 5
		object.value6 = object.value0
		object.value6 >>= 1
		object.value6 += 17
		if object.value0 < 12
			object.value0++
		else
			object.value0 = 0
			object.state++
		end if
		break

	case 3
		if object.value0 < 300
			object.value0++
			if object.value0 > 240
				music.volume -= 2
			end if
		else
			PlayMusic(1)
			object[30].type = TypeName[Act Finish]
			object[30].drawOrder = 6
			object[30].priority = PRIORITY_ACTIVE
			object.value0 = 0
			object.state++
		end if
		break

	// State 4 is a dummy state

	end switch

	object.value2 = object.ypos
	object.value2 -= 0x300000

	temp2 = object.value2
	temp2 += 0x80000

	// Check collision with players
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		BoxCollisionTest(C_BOX, object.entityPos, -32, -24, 32, 32, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
		BoxCollisionTest(C_BOX, object.entityPos, -16, -40, 16, -24, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
		if object.state == 0
			if checkResult == 1
				object.state = 1
				stage.timeEnabled = false
				if PlayerObject_SuperState == 1
					PlayerObject_SuperState = 2
				end if
				object.xvel = 0x80000
				object.yvel = -0x40000
				object.value3 = object.xpos
				object.value4 = object.ypos
				CreateTempObject(TypeName[Smoke Puff], 0, object.xpos, object.ypos)
				object[tempObjectPos].ypos -= 0x180000
				PlaySfx(SfxName[Destroy], 0)
			end if
		end if
		BoxCollisionTest(C_TOUCH, object.entityPos, -15, -48, 15, -24, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
		if checkResult == true
			temp0 = object[currentPlayer].collisionBottom
			temp0 <<= 16
			temp0 += object[currentPlayer].ypos
			if temp0 > object.value2
				if temp0 <= temp2
					object.value2 = temp0
				end if
			end if
		end if
		temp0 = object[currentPlayer].collisionRight
		temp0 <<= 16
		temp0 += object[currentPlayer].xpos
		temp1 = stage.curXBoundary2
		temp1 <<= 16
		if temp0 > temp1
			object[currentPlayer].xvel = 0
			object[currentPlayer].speed = 0
			object[currentPlayer].xpos = temp1
			temp0 = object[currentPlayer].collisionRight
			temp0 <<= 16
			object[currentPlayer].xpos -= temp0
		end if
	next
end event


event ObjectDraw
	switch object.state
	case 0
		DrawSpriteXY(2, object.xpos, object.value2)
		DrawSprite(0)
		DrawSprite(4)
		break
	case 1
		DrawSpriteXY(2, object.xpos, object.value2)
		DrawSprite(0)
		DrawSpriteXY(4, object.value3, object.value4)
		break
	case 2
		DrawSpriteXY(2, object.xpos, object.value2)
		DrawSprite(1)
		DrawSprite(object.value5)
		object.direction = FLIP_X
		DrawSpriteFX(object.value5, FX_FLIP, object.xpos, object.ypos)
		DrawSprite(object.value6)
		break
	case 3
	case 4
		DrawSpriteXY(2, object.xpos, object.value2)
		DrawSprite(1)
		break
	end switch
end event


event ObjectStartup
	LoadSpriteSheet("Global/AnimalPrison.gif")
	SpriteFrame(-32, -32, 64, 64, 1, 1)
	SpriteFrame(-32, -32, 64, 64, 148, 1)
	SpriteFrame(-16, 0, 32, 16, 213, 1)
	SpriteFrame(-16, -48, 32, 16, 213, 1)
	SpriteFrame(-8, -31, 16, 16, 213, 18)
	SpriteFrame(-32, -16, 24, 24, 83, 17)
	SpriteFrame(-32, -14, 24, 22, 83, 17)
	SpriteFrame(-32, -12, 24, 20, 83, 17)
	SpriteFrame(-32, -10, 24, 18, 83, 17)
	SpriteFrame(-32, -8, 24, 16, 83, 17)
	SpriteFrame(-32, -6, 24, 14, 83, 17)
	SpriteFrame(-32, -4, 24, 12, 83, 17)
	SpriteFrame(-32, -2, 24, 10, 83, 17)
	SpriteFrame(-32, 0, 24, 8, 83, 17)
	SpriteFrame(-32, 2, 24, 6, 83, 17)
	SpriteFrame(-32, 4, 24, 4, 83, 17)
	SpriteFrame(-32, 6, 24, 2, 83, 17)
	SpriteFrame(-8, -16, 16, 24, 107, 17)
	SpriteFrame(-8, -16, 16, 20, 107, 17)
	SpriteFrame(-8, -16, 16, 16, 107, 17)
	SpriteFrame(-8, -16, 16, 12, 107, 17)
	SpriteFrame(-8, -16, 16, 8, 107, 17)
	SpriteFrame(-8, -16, 16, 4, 107, 17)
	
	// Add this object to the debug item list
	SetTableValue(TypeName[Animal Prison], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(AnimalPrison_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(AnimalPrison_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++

	// Give all Animal Prisons the XBOUNDS priority
	foreach (TypeName[Animal Prison], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].priority = PRIORITY_XBOUNDS
	next

	// Erase all aniaml pricons if in VS mode
	if options.vsMode == true
		foreach (TypeName[Animal Prison], arrayPos0, ALL_ENTITIES)
			object[arrayPos0].type = TypeName[Blank Object]
		next
	end if
	
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
